package com.test.lua;

import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:yaoxiaojian
 * @Description:
 * @Date:create in 2020/5/26 16:43
 * @Modified By:
 */
public class LuaDemo {
	/**
	 * 这是将脚本提取到外面为常量，用 jedis.evalsha()加载
	 */
	public static void main(String[] args) {
		Jedis jedis = RedisManager.getJedis();
		List<String> keys = new ArrayList(){{
			 add("stock_skuNo_soldNum_SKU031518110000000578");
			 add("stock_skuNo_lockNum_SKU031518110000000578");
			 add("lockStock_orderDetail_e25a356f0d6547fbb9fda5566c9439e9_SKU031518110000000578");
			 add("SKU031518110000000578");
			 add("2");
		}};
		Object obj = jedis.eval(getOrderUnLockLua1(),keys,new ArrayList<String>());
		System.out.println(obj);
	}

	/**
	 * 获取普通订单解锁lua  脚本
	 * @return
	 */
	public static String getOrderUnLockLua1() {
		String lua="local  flag = true\n" +
				"local soldNum = 0\n" +
				"local stockLockNum = 0\n" +
				"local orderDetailLockNum = 0\n" +
				"local unLockNum = 0\n" +
				"local promotionLockNum = 0\n" +
				"local promotionLockKey = ''\n" +
				" local array = {} \n"+
				"-- key:skuNo  value:unLockNum\n" +
				"local tab={}\n" +
				"-- 校验订单解锁信息\n" +
				" local i = 1 \n"+
				"for key,value in ipairs(KEYS)\n" +
				"\tdo\n" +
				"\tflag=false \n" +
				"\t i = i+1 \n"+
				"end\n" +
				"\t return flag";
				/*"for key,value in ipairs(ARGV)\n" +
				"\tdo\n" +
				"\tif key % 5 ==1 or key % 5 ==2 then  \n" +
				"\t\tif tonumber(redis.call('EXISTS',value))==0 then  flag=false end\n" +
				"\tend\n" +
				"end\n" +
				"-- 校验解锁数量\n" +
				"if flag then\n" +
				"\tfor key,value in ipairs(KEYS)\n" +
				"\t\tdo\n" +
				"\t\tif not flag then break end\n" +
				"\t\tif key % 5 ==2 then  stockLockNum= tonumber(redis.call('get',value))  end\n" +
				"\t\tif key % 5 ==3 then  orderDetailLockNum= tonumber(redis.call('get',value))  end\n" +
				"\t\tif key % 5 ==0 then  unLockNum=tonumber(value)\n" +
				"\t\t\ttab[KEYS[key-1]]=unLockNum\t\n" +
				"\t\t\tif unLockNum > stockLockNum or unLockNum > orderDetailLockNum then flag=false end\n" +
				"\t\tend\n" +
				"\tend\n" +
				"end\n" +
				"\n" +
				"if flag then\n" +
				"\tfor key,value in ipairs(ARGV)\n" +
				"\t\tdo\n" +
				"\t\tif not flag then break end\n" +
				"\t\tif key % 5 ==2 then  stockLockNum= tonumber(redis.call('get',value))  end\n" +
				"\t\tif key % 5 ==4 then \n" +
				"\t\t\tif tonumber(redis.call('EXISTS',value))==1 then\n" +
				"\t\t\t\tif tab[ARGV[key+1]] ~=nil\tthen \n" +
				"\t\t\t\t\tunLockNum= tonumber(tab[ARGV[key+1]]+redis.call('get',value))\n" +
				"\t\t\t\telse\n" +
				"\t\t\t\t\tunLockNum=tonumber(redis.call('get',value)) \n" +
				"\t\t\t\tend\n" +
				"\t\t\t\tif unLockNum > stockLockNum then flag=false end\n" +
				"\t\t\tend\n" +
				"\t\tend\n" +
				"\tend\n" +
				"end\n" +
				"-- 解锁\n" +
				"if flag then\n" +
				"\tfor key,value in ipairs(KEYS)\n" +
				"\t\tdo\n" +
				"\t\tif key % 5 ==1 then  soldNum= tonumber(redis.call('get',value))  end\n" +
				"\t\tif key % 5 ==2 then  stockLockNum= tonumber(redis.call('get',value))  end\n" +
				"\t\tif key % 5 ==3 then  orderDetailLockNum= tonumber(redis.call('get',value))  end\n" +
				"\t\tif key % 5 ==0 then  unLockNum=tonumber(value)\n" +
				"\t\t\tredis.call('set',KEYS[key-4],soldNum+unLockNum)\n" +
				"\t\t\tredis.call('set',KEYS[key-3],stockLockNum-unLockNum)\n" +
				"\t\t\tif orderDetailLockNum > unLockNum then\n"+
				"\t\t\t\tredis.call('set',KEYS[key-2],orderDetailLockNum-unLockNum)\n"+
				"\t\t\telse\n"+
				"\t\t\t\tredis.call('del',KEYS[key-2])\n"+
				"\t\t\tend\n"+
				"\t\tend\n" +
				"\tend\n" +
				"\n" +
				"\tfor key,value in ipairs(ARGV)\n" +
				"\t\tdo\n" +
				"\t\tif key % 5 ==1 then  soldNum= tonumber(redis.call('get',value))  end\n" +
				"\t\tif key % 5 ==2 then  stockLockNum= tonumber(redis.call('get',value))  end\n" +
				"\t\tif key % 5 ==3 then  promotionLockKey=value end\n" +
				"\t\tif key % 5 ==4 then \n" +
				"\t\t\tif tonumber(redis.call('EXISTS',value))==1 then \n" +
				"\t\t\t\tunLockNum=tonumber(redis.call('get',value))\n" +
				"\t\t\t\tif tonumber(redis.call('EXISTS',promotionLockKey))==1 then\n" +
				"\t\t\t\t\t-- 还原到促销锁库\n" +
				"\t\t\t\t\tpromotionLockNum= tonumber(redis.call('get',promotionLockKey))\n" +
				"\t\t\t\t\tredis.call('set',promotionLockKey,promotionLockNum+unLockNum)\n" +
				"\t\t\t\telse\n" +
				"\t\t\t\t\t-- 还原到库存中\n" +
				"\t\t\t\t\tredis.call('set',ARGV[key-3],soldNum+unLockNum)\n" +
				"\t\t\t\t\tredis.call('set',ARGV[key-2],stockLockNum-unLockNum)\n" +
				"\t\t\t\tend\n" +
				"\t\t\t\tredis.call('del',value)\t\n" +
				"\t\t\tend\n" +
				"\t\tend\n" +
				"\tend\n" +
				"end\n" +
				"return flag";*/
		return lua;
	}

	/**
	 * 获取普通订单解锁lua  脚本
	 * @return
	 */
	public static String getOrderUnLockLua() {
		String lua="local  flag = true\n" +
				"local soldNum = 0\n" +
				"local stockLockNum = 0\n" +
				"local orderDetailLockNum = 0\n" +
				"local unLockNum = 0\n" +
				"\n" +
				"local promotionLockNum = 0\n" +
				"\n" +
				"local promotionLockKey = ''\n" +
				"-- key:skuNo  value:unLockNum\n" +
				"local tab={}\n" +
				"-- 校验订单解锁信息\n" +
				"for key,value in ipairs(KEYS)\n" +
				"\tdo\n" +
				"\tif key % 5 ~=0  and key % 5 ~=4 then  \n" +
				"\t\tif tonumber(redis.call('EXISTS',value))==0 then  flag=false end\n" +
				"\tend\n" +
				"end\n" +
				"for key,value in ipairs(ARGV)\n" +
				"\tdo\n" +
				"\tif key % 5 ==1 or key % 5 ==2 then  \n" +
				"\t\tif tonumber(redis.call('EXISTS',value))==0 then  flag=false end\n" +
				"\tend\n" +
				"end\n" +
				"-- 校验解锁数量\n" +
				"if flag then\n" +
				"\tfor key,value in ipairs(KEYS)\n" +
				"\t\tdo\n" +
				"\t\tif not flag then break end\n" +
				"\t\tif key % 5 ==2 then  stockLockNum= tonumber(redis.call('get',value))  end\n" +
				"\t\tif key % 5 ==3 then  orderDetailLockNum= tonumber(redis.call('get',value))  end\n" +
				"\t\tif key % 5 ==0 then  unLockNum=tonumber(value)\n" +
				"\t\t\ttab[KEYS[key-1]]=unLockNum\t\n" +
				"\t\t\tif unLockNum > stockLockNum or unLockNum > orderDetailLockNum then flag=false end\n" +
				"\t\tend\n" +
				"\tend\n" +
				"end\n" +
				"\n" +
				"if flag then\n" +
				"\tfor key,value in ipairs(ARGV)\n" +
				"\t\tdo\n" +
				"\t\tif not flag then break end\n" +
				"\t\tif key % 5 ==2 then  stockLockNum= tonumber(redis.call('get',value))  end\n" +
				"\t\tif key % 5 ==4 then \n" +
				"\t\t\tif tonumber(redis.call('EXISTS',value))==1 then\n" +
				"\t\t\t\tif tab[ARGV[key+1]] ~=nil\tthen \n" +
				"\t\t\t\t\tunLockNum= tonumber(tab[ARGV[key+1]]+redis.call('get',value))\n" +
				"\t\t\t\telse\n" +
				"\t\t\t\t\tunLockNum=tonumber(redis.call('get',value)) \n" +
				"\t\t\t\tend\n" +
				"\t\t\t\tif unLockNum > stockLockNum then flag=false end\n" +
				"\t\t\tend\n" +
				"\t\tend\n" +
				"\tend\n" +
				"end\n" +
				"-- 解锁\n" +
				"if flag then\n" +
				"\tfor key,value in ipairs(KEYS)\n" +
				"\t\tdo\n" +
				"\t\tif key % 5 ==1 then  soldNum= tonumber(redis.call('get',value))  end\n" +
				"\t\tif key % 5 ==2 then  stockLockNum= tonumber(redis.call('get',value))  end\n" +
				"\t\tif key % 5 ==3 then  orderDetailLockNum= tonumber(redis.call('get',value))  end\n" +
				"\t\tif key % 5 ==0 then  unLockNum=tonumber(value)\n" +
				"\t\t\tredis.call('set',KEYS[key-4],soldNum+unLockNum)\n" +
				"\t\t\tredis.call('set',KEYS[key-3],stockLockNum-unLockNum)\n" +
				"\t\t\tif orderDetailLockNum > unLockNum then\n"+
				"\t\t\t\tredis.call('set',KEYS[key-2],orderDetailLockNum-unLockNum)\n"+
				"\t\t\telse\n"+
				"\t\t\t\tredis.call('del',KEYS[key-2])\n"+
				"\t\t\tend\n"+
				"\t\tend\n" +
				"\tend\n" +
				"\n" +
				"\tfor key,value in ipairs(ARGV)\n" +
				"\t\tdo\n" +
				"\t\tif key % 5 ==1 then  soldNum= tonumber(redis.call('get',value))  end\n" +
				"\t\tif key % 5 ==2 then  stockLockNum= tonumber(redis.call('get',value))  end\n" +
				"\t\tif key % 5 ==3 then  promotionLockKey=value end\n" +
				"\t\tif key % 5 ==4 then \n" +
				"\t\t\tif tonumber(redis.call('EXISTS',value))==1 then \n" +
				"\t\t\t\tunLockNum=tonumber(redis.call('get',value))\n" +
				"\t\t\t\tif tonumber(redis.call('EXISTS',promotionLockKey))==1 then\n" +
				"\t\t\t\t\t-- 还原到促销锁库\n" +
				"\t\t\t\t\tpromotionLockNum= tonumber(redis.call('get',promotionLockKey))\n" +
				"\t\t\t\t\tredis.call('set',promotionLockKey,promotionLockNum+unLockNum)\n" +
				"\t\t\t\telse\n" +
				"\t\t\t\t\t-- 还原到库存中\n" +
				"\t\t\t\t\tredis.call('set',ARGV[key-3],soldNum+unLockNum)\n" +
				"\t\t\t\t\tredis.call('set',ARGV[key-2],stockLockNum-unLockNum)\n" +
				"\t\t\t\tend\n" +
				"\t\t\t\tredis.call('del',value)\t\n" +
				"\t\t\tend\n" +
				"\t\tend\n" +
				"\tend\n" +
				"end\n" +
				"return flag";
		return lua;
	}


}
